/**
 * 资讯中心 html 静态页，生成 
 * **/	

package com.wsj.web.action.siteindex;

import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.util.tool.HtmlCreateObj;
import com.wsj.util.tool.RequestUtils;

public class HomeSiteNews_HTML_Action extends Action{

	/** 生成html页面对象 **/	
	private HtmlCreateObj htmlCreateObj;
	
	public  ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		// 生成静态页面
		String filePath = "index.html";
		
		//		检查生成的文件路径是否存在,如果不存在.则创建该文件路径.
		String host = RequestUtils.getRequestHost(request);
		String oldPath = host + "/html/newsIndex/index.html";
		//File fileDir = new File(oldPath);
		
		HttpURLConnection.setFollowRedirects(false);   
        HttpURLConnection   con   = (HttpURLConnection)   new   URL(oldPath).openConnection();   
        con.setRequestMethod("HEAD");
		String create=request.getParameter("create");
			
		//检查文件是否存在,否则创建文件 !fileDir.exists() 只能检测本地文件是否存在
		if(!(con.getResponseCode() == HttpURLConnection.HTTP_OK))
		{
			String url = RequestUtils.getRequestHost(request)+"/newsIndex.do";
			htmlCreateObj.setCreateFileParament(url, "html/newsIndex/", filePath);
			htmlCreateObj.createHtmlFile(request, response);
		}
		else
		{
			if(create!=null  && create.equals("create"))
			{
				String url = RequestUtils.getRequestHost(request)+"/newsIndex.do?nav=1";
				htmlCreateObj.setCreateFileParament(url, "html/newsIndex/", filePath);
				htmlCreateObj.createHtmlFile(request, response);
			}
		}
		
		
		return mapping.findForward("zxzxIndex");
		
	}
	

	public HtmlCreateObj getHtmlCreateObj() {
		return htmlCreateObj;
	}

	public void setHtmlCreateObj(HtmlCreateObj htmlCreateObj) {
		this.htmlCreateObj = htmlCreateObj;
	}
	
	
}
